import { createSlice } from '@reduxjs/toolkit'
import AsyncStorage from '@react-native-async-storage/async-storage';
import { realm, schemaName } from "../../utils/schema"
import NetworkList from "../../utils/NetworkList"

const initialState = {
    isWallet: false,
    Wallet: [],
    WalletList: [],
    NetworkList: NetworkList,
}
export const walletSlice = createSlice({
    name: 'wallet',
    initialState,
    reducers: {
        SetWallet: (state, action) => {
            state.Wallet = action.payload
        },
        SetWalletList: (state, action) => {
            state.WalletList = action.payload
        },
        SetIsWallet: (state, action) => {
            state.isWallet = action.payload ? true : false
        },
        SetNetworkRpc: (state, action) => {
            state.NetworkList = action.payload
        },
    },
})

export const { SetWallet, SetIsWallet, SetWalletList, SetNetworkRpc } = walletSlice.actions

export const asyncSetCurrentWallet = (Wallet) => async (dispatch) => {
    const { id, address, keystore, type, chainId, name, balance, symbol, decimals, state } = Wallet
    realm.write(() => {
        realm.create(schemaName.CurrentWalle, {
            id,
            address,
            keystore: keystore.address ? JSON.stringify(keystore) : keystore,
            type,
            chainId,
            name,
            balance,
            symbol,
            decimals,
            state
        }, true);

    })
    const wallet = realm.objects(schemaName.CurrentWalle)
    dispatch(SetWallet(JSON.parse(JSON.stringify(wallet[0] || []))));
}

export const asyncSetwallet = (Wallet) => async (dispatch) => {
    const { id, address, keystore, type, chainId, name, balance, symbol, decimals, state } = Wallet
    let WalletListAll = realm.objects(schemaName.WalletList)
    let WalletList = WalletListAll.filtered(`state == 1`)

    realm.write(() => {
        realm.create(schemaName.WalletList, {
            id: id || WalletListAll.length + 1,
            address,
            keystore: keystore.address ? JSON.stringify(keystore) : keystore,
            type,
            chainId,
            name,
            balance,
            symbol,
            decimals,
            state
        }, true);
        realm.create(schemaName.CurrentWalle, {
            id: id || WalletListAll.length,
            address,
            keystore: keystore.address ? JSON.stringify(keystore) : keystore,
            type,
            chainId,
            name,
            balance,
            symbol,
            decimals,
            state
        }, true);
    })
    const CurrentWalle = realm.objects(schemaName.CurrentWalle)
    const wallet = JSON.parse(JSON.stringify(CurrentWalle[0]))
    dispatch(SetWallet(wallet));
    const WalletListConvert = JSON.parse(JSON.stringify(WalletList))
    WalletListConvert.forEach(item => item.keystore = JSON.parse(item.keystore))
    dispatch(SetWalletList(WalletListConvert));
};

export const asyncNetworkRpc = () => async (dispatch) => {
    const list = []
    let RpcUrlList = await realm.objects(schemaName.CurrentRpcUrl);
    NetworkList.forEach(item => {
        const rpcUrlItem = RpcUrlList.find(ite => ite.chainId == item.chainId)
        if (rpcUrlItem) {
            list.push({ ...item, rpcUrl: rpcUrlItem.rpcUrl })
        } else {
            list.push(item)
        }
    })
    dispatch(SetNetworkRpc(list))
};


export default walletSlice.reducer